<?php
/*
  echo "<pre>";
  foreach ($data['1'] as $row) {
  echo $row->menu_title."=>";
  foreach ($data['2'][$row->menu_id] as $row_2) {
  echo $row_2->menu_title;
  echo "(";
  foreach ($data['3'][$row_2->menu_id] as $row_3) {
  echo $row_3->menu_title.",";
  }
  echo ")";
  }
  echo "<br>";
  }
  //die($data->menu_title);
  //die($dataProvider->getData());
 * 
 */

$this->breadcrumbs = array(
    'Content',
    'Menu' => array('index'),
);

$this->menu = array();

//action create
//if ($this->action_create == 1) {
//    array_push($this->menu, array('label' => 'Create Menu', 'url' => array('create')));
//}


Yii::app()->clientScript->registerScriptFile(Yii::app()->params['backendUrl'] . '/js/jqueryui/1.8.16/jquery-ui.min.js');

$str_js = "
        var fixHelper = function(e, ui) {
            ui.children().each(function() {
                $(this).width($(this).width());
            });
            return ui;
        };

        $('#menu-grid table.items ').sortable({
            cursor:     'move',
            axis:       'y',
            containment:'tbody.sub',
            forcePlaceholderSize: true,
            forceHelperSize: true,
            items:'tr',
            helper: fixHelper
        }).disableSelection();
        
    ";

Yii::app()->clientScript->registerScript('sortable-project', $str_js);
?>
<div class="flat_area grid_16">
    <h2>List of Public Menu</h2>
</div>
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->params['backendUrl']; ?>/css/style-gridview.css" />
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->params['backendUrl']; ?>/css/style-pager.css" />

<div id="menu-grid" class="grid-view">
    <div class="summary">Displaying 1-6 of 6 result(s).</div>
    <!--
    <form action="" method="post" name="list">
    <input type="submit" name="form_add" value="Tambah" class="button" />&nbsp;
    <input type="submit" name="publish" value="Publish" class="button" />&nbsp;
    <input type="submit" name="unpublish" value="Unpublish" class="button" />&nbsp;
    <input type="submit" name="delete" value="Delete" class="button" />
    <br /><br />
    -->
    <table class="items">
        <thead>
            <tr>
                <th width="20"><input class="checkbox" type="checkbox" name="item" id="item" onclick="check_all('list', 'item', 
                    <?php
                    $no = 0;
                    echo $no;
                    ?>)" /></th>
                <th width="20">#</th>
                <th colspan="3" width="20">Judul</th>
                <th width="20">Publish</th>
                <th width="80">Action</th>
            </tr>
            <tr class="filters">
                <td colspan="8">&nbsp</td>
            </tr>
        </thead>

        <?php
        $i = 1;
        if (count($data['1']) > 0) {
            $no = 1;
            foreach ($data['1'] as $row) {
                $is_active = ($row->menu_is_active == '1') ? 'stat-active.png' : 'stat-inactive.png';

                if ($i == 1)
                    $order_by = '<input type="image" name="" value="" src="' . Yii::app()->request->baseUrl . '/images/icon/order-up-inactive.png" title="Up" class="button-image" id="par_up' . $row->menu_id . '" disabled><input type="image" name="order[' . $row->menu_id . ']" value="down" src="' . Yii::app()->request->baseUrl . '/images/icon/order-down-active.png" title="Down" class="button-image" id="down_' . $row->menu_id . '">';
                elseif ($i == count($data['1']))
                    $order_by = '<input type="image" name="order[' . $row->menu_id . ']" value="up" src="' . Yii::app()->request->baseUrl . '/images/icon/order-up-active.png" title="Up" class="button-image" id="up_' . $row->menu_id . '"><input type="image" name="" value="" src="' . Yii::app()->request->baseUrl . '/images/icon/order-down-inactive.png" title="Down" class="button-image" id="down_' . $row->menu_id . '" disabled>';
                else
                    $order_by = '<input type="image" name="order[' . $row->menu_id . ']" value="up" src="' . Yii::app()->request->baseUrl . '/images/icon/order-up-active.png" title="Up" class="button-image" id="up_' . $row->menu_id . '"><input type="image" name="order[' . $row->menu_id . ']" value="down" src="' . Yii::app()->request->baseUrl . '/images/icon/order-down-active.png" title="Down" class="button-image" id="down_' . $row->menu_id . '">';

                echo '<tbody class="content">';
                ?>
                <tr class="trroot">
                    <td align="center"><input type="checkbox" name="item[<?php echo $no; ?>]" id="item[<?php echo $no; ?>]" value="<?php echo $row->menu_id; ?>"></td>
                    <td width="40" align="right"><?php echo $order_by; ?></td>
                    <td colspan="3"><strong><?php echo $row->menu_title; ?></strong></td>
                    <td align="center"><img src="<?php echo Yii::app()->request->baseUrl; ?>/images/icon/<?php echo $is_active; ?>" alt="<?php echo $is_active; ?>" /></td>
                    <td align="center">
                        <a href="<?php echo $this->createUrl('menu/update/') . "/" . $row->menu_id; ?>"><img src="<?php echo Yii::app()->request->baseUrl; ?>/images/icon/adm_edit_navy.png" alt="Update" border="0" /></a>&nbsp;
                        <?php
                        echo CHtml::ajaxLink(
                            '<img src="' . Yii::app()->request->baseUrl . '/images/icon/adm_delete_navy.png" alt="Delete" border="0" />', 
                            $this->createUrl('menu/delete/', array('id' => $row->menu_id)), 
                            array(
                                'type' => POST, 
                                'data' => array('ajax' => 'menu-grid'), 
                                'success' => 'js:function(data){
                                    if(data == 1) {
                                        window.location.reload();
                                    } else {
                                        alert("Deleting Data has failed");
                                    }
                                }',
                            ),
                            array(
                                'confirm' => 'Are you sure you want to delete this item?', 
                                'href' => $this->createUrl('menu/delete/', array('id' => $row->menu_id)))
                        );
                        ?>
                    </td>
                </tr>
                </tbody>
        <?php
        if (count($data['2']) > 0) {
            $no_sub = 1;

            $x = 1;
            foreach ($data['2'][$row->menu_id] as $row_2) {
                if ($x == 1)
                    $order_by = '<input type="image" name="" value="" src="' . Yii::app()->request->baseUrl . '/images/icon/order-up-inactive.png" title="Up" class="button-image" id="par_up' . $row_2->menu_id . '" disabled><input type="image" name="order[' . $row_2->menu_id . ']" value="down" src="' . Yii::app()->request->baseUrl . '/images/icon/order-down-active.png" title="Down" class="button-image" id="down_' . $row_2->menu_id . '">';
                elseif ($x == count($data['2'][$row->menu_id]))
                    $order_by = '<input type="image" name="order[' . $row->menu_id . ']" value="up" src="' . Yii::app()->request->baseUrl . '/images/icon/order-up-active.png" title="Up" class="button-image" id="up_' . $row_2->menu_id . '"><input type="image" name="" value="" src="' . Yii::app()->request->baseUrl . '/images/icon/order-down-inactive.png" title="Down" class="button-image" id="down_' . $row_2->menu_id . '" disabled>';
                else
                    $order_by = '<input type="image" name="order[' . $row->menu_id . ']" value="up" src="' . Yii::app()->request->baseUrl . '/images/icon/order-up-active.png" title="Up" class="button-image" id="up_' . $row_2->menu_id . '"><input type="image" name="order[' . $row->menu_id . ']" value="down" src="' . Yii::app()->request->baseUrl . '/images/icon/order-down-active.png" title="Down" class="button-image" id="down_' . $row_2->menu_id . '">';

                $is_active_submenu = ($row_2->menu_is_active == '1') ? 'stat-active.png' : 'stat-inactive.png';
                echo '<tbody class="sub">';
                ?>
                        <tr class="trsub">
                            <td align="center"><input type="checkbox" name="item[<?php echo $no; ?>]" id="item[<?php echo $no; ?>]" value="<?php echo $row_2->menu_id; ?>"></td>
                            <td>&nbsp;</td>
                            <td width="40" align="right"><?php echo $order_by; ?></td>
                            <td colspan="2"><strong><?php echo $row_2->menu_title; ?></strong></td>
                            <td align="center"><img src="<?php echo Yii::app()->request->baseUrl; ?>/images/icon/<?php echo $is_active_submenu; ?>" alt="<?php echo $is_active_submenu; ?>" /></td>
                            <td align="center">
                                <a href="<?php echo $this->createUrl('menu/update/') . "/" . $row_2->menu_id; ?>"><img src="<?php echo Yii::app()->request->baseUrl; ?>/images/icon/adm_edit_navy.png" alt="Update" border="0" /></a>&nbsp;
                                <?php
                                echo CHtml::ajaxLink(
                                    '<img src="' . Yii::app()->request->baseUrl . '/images/icon/adm_delete_navy.png" alt="Delete" border="0" />', 
                                    $this->createUrl('menu/delete/', array('id' => $row_2->menu_id)), 
                                    array(
                                        'type' => POST, 
                                        'data' => array('ajax' => 'menu-grid'), 
                                        'success' => 'js:function(data){
                                            if(data == 1) {
                                                window.location.reload();
                                            } else {
                                                alert("Deleting Data has failed");
                                            }
                                        }',
                                    ),
                                    array(
                                        'confirm' => 'Are you sure you want to delete this item?', 
                                        'href' => $this->createUrl('menu/delete/', array('id' => $row_2->menu_id)))
                                );
                                ?>
                            </td>
                        </tr>
                        </tbody>
                <?php
                if (count($data['3']) > 0) {
                    $no_sub2 = 1;

                    $y = 1;
                    foreach ($data['3'][$row_2->menu_id] as $row_3) {
                        if ($y == 1)
                            $order_by = '<input type="image" name="" value="" src="' . Yii::app()->request->baseUrl . '/images/icon/order-up-inactive.png" title="Up" class="button-image" id="par_up' . $row_2->menu_id . '" disabled><input type="image" name="order[' . $row_3->menu_id . ']" value="down" src="' . Yii::app()->request->baseUrl . '/images/icon/order-down-active.png" title="Down" class="button-image" id="down_' . $row_3->menu_id . '">';
                        elseif ($y == count($data['3'][$row_2->menu_id]))
                            $order_by = '<input type="image" name="order[' . $row->menu_id . ']" value="up" src="' . Yii::app()->request->baseUrl . '/images/icon/order-up-active.png" title="Up" class="button-image" id="up_' . $row_3->menu_id . '"><input type="image" name="" value="" src="' . Yii::app()->request->baseUrl . '/images/icon/order-down-inactive.png" title="Down" class="button-image" id="down_' . $row_3->menu_id . '" disabled>';
                        else
                            $order_by = '<input type="image" name="order[' . $row->menu_id . ']" value="up" src="' . Yii::app()->request->baseUrl . '/images/icon/order-up-active.png" title="Up" class="button-image" id="up_' . $row_3->menu_id . '"><input type="image" name="order[' . $row->menu_id . ']" value="down" src="' . Yii::app()->request->baseUrl . '/images/icon/order-down-active.png" title="Down" class="button-image" id="down_' . $row_3->menu_id . '">';

                        $is_active_submenu = ($row_2->menu_is_active == '1') ? 'stat-active.png' : 'stat-inactive.png';
                        echo '<tbody class="sub2">';
                        ?>
                                <tr class="trsub2">
                                    <td align="center"><input type="checkbox" name="item[<?php echo $no; ?>]" id="item[<?php echo $no; ?>]" value="<?php echo $row_3->menu_id; ?>"></td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td width="40" align="right"><?php echo $order_by; ?></td>
                                    <td><?php echo $row_3->menu_title; ?></td>
                                    <td align="center"><img src="<?php echo Yii::app()->request->baseUrl; ?>/images/icon/<?php echo $is_active_submenu; ?>" alt="<?php echo $is_active_submenu; ?>" /></td>
                                    <td align="center">
                                        <a href="<?php echo $this->createUrl('menu/update/') . "/" . $row_3->menu_id; ?>"><img src="<?php echo Yii::app()->request->baseUrl; ?>/images/icon/adm_edit_navy.png" alt="Update" border="0" /></a>&nbsp;
                                        <?php
                                        echo CHtml::ajaxLink(
                                            '<img src="' . Yii::app()->request->baseUrl . '/images/icon/adm_delete_navy.png" alt="Delete" border="0" />', 
                                            $this->createUrl('menu/delete/', array('id' => $row_3->menu_id)), 
                                            array(
                                                'type' => POST, 
                                                'data' => array('ajax' => 'menu-grid'), 
                                                'success' => 'js:function(data){
                                                    if(data == 1) {
                                                        window.location.reload();
                                                    } else {
                                                        alert("Deleting Data has failed");
                                                    }
                                                }',
                                            ),
                                            array(
                                                'confirm' => 'Are you sure you want to delete this item?', 
                                                'href' => $this->createUrl('menu/delete/', array('id' => $row_3->menu_id)))
                                        );
                                        ?>
                                    </td>
                                </tr>
                                        <?php
                                        $y++;
                                        $no_sub2++;
                                        echo '</tbody>';
                                    }

                                    $no_sub++;
                                }
                                $x++;
                                echo '</tbody>';
                            }

                            $no++;
                        }
                        $i++;
                        echo '</tbody>';
                    }
                }
                else
                    echo '<tr class="eventList"><td colspan="6" align="center">---=====---</td></tr>';
                ?>


    </table>
    <!--
    </form>
    -->
</div>
<script>
    jQuery.each($('.button-image'), function(i, val) {
        var id = $(this).attr('id');
        // alert(id);
        $('#'+id).click(function()  {
            $.ajax({
                type: 'POST',
                url: '<?php echo $this->createUrl('//admin/content/menu/order_by'); ?>',
                data: 'id='+id,
                success: function(){
                    location.reload(true);
                }
            });
        });
    });
</script>
